<!-- @module attendance-info -->
<!-- @author: YunTao.Li -->
<!-- @description: 考勤信息 -->
<!-- @since: 2019-11-06 17:56:48 -->

<template>
  <el-card class="box-card el-card-flex">
  <div slot="header" class="clearfix">
      <span>
        <i class="el-icon-position"></i>
         查询信息
      </span>
  </div>
    <!-- form begin -->
    <div style="margin: 10px">
    <el-form ref="queryForm" label-width="120px" class="div-header" :model="queryForm" :rules="queryFormRules" size="mini">

        <el-row>
          <el-col :xs="24" :sm="12" :md="7" :lg="7" :xl="7">
            <el-form-item label="组织机构" prop="org">
              <OrgSelectTree
                style="width: 240px"
                ref="orgSelectRef"
                v-model="queryForm.orgCode"
                placeholder="组织机构"
                :disabled="input_org_disabled"
                @bindLabel="bindLabel"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="7" :lg="7" :xl="7">
            <el-form-item label="部门选择" prop="deptName">
              <DeptSelectTree
                style="width: 240px"
                ref="deptSelectRef"
                v-model="queryForm.deptCode"
                placeholder="部门下拉"
                :parent-org-id="queryForm.ouId"
                :default-dept-name="queryForm.deptName"
                :disabled="input_dept_disabled"
                @bindLabel="bindDeptLabel"
              />
              <!-- <el-input
                ref="deptInputRef"
                v-model="queryForm.deptName"
                placeholder="部门名称"
                :disabled="input_dept_disabled"
              /> -->
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="7" :lg="7" :xl="7">
            <el-form-item label="考勤状态" prop="confirmStatus">
              <dictCombo
                style="width: 240px"
                v-model="queryForm.confirmStatus"
                dicttypeid="confirmStatus"
                name="confirmStatus"
                placeholder="考勤状态"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col v-if="empInputShow" :xs="24" :sm="12" :md="7" :lg="7" :xl="7">
            <el-form-item label="员工姓名" prop="empCode">
              <EmpCombobox
                style="width: 240px"
                v-model="queryForm.empCode"
                name="empCode"
                placeholder="员工姓名"
                :disabled="input_emp_disabled"
              />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="7" :lg="7" :xl="7">
            <el-form-item label="开始时间" prop="beginDate">
              <el-date-picker
                style="width: 240px"
                v-model="queryForm.beginDate"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptionsStart"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="7" :lg="7" :xl="7">
            <el-form-item label="结束时间" prop="endDate">
              <el-date-picker
                style="width: 240px"
                v-model="queryForm.endDate"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptionsEnd"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="btnStyle">
          <el-button  type="primary"
                      size="small"
                      v-if="appPath==1"
                      @click="goBackWorkbench()">返回</el-button>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList()">查询</el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-refresh"
            @click="resetForm('queryForm')"
          >重置</el-button>
        </div>
    </el-form>
    </div>
      <!-- form end -->
      <div class="div-button">
        <el-form ref="syncQueryForm" :model="syncQueryForm" label-width="120px" size="mini">
          <el-button
            icon="el-icon-refresh"
            type="primary"
            size="mini"
            :disabled.sync="btn_sync_disabled"
            @click="onDataSync()"
          >考勤同步</el-button>
        </el-form>
      </div>
      <!-- table begin-->
      <div class="el-flex el-box-column " style="margin: 10px;">
        <el-card class="box-card el-card-flex" >
          <div slot="header" class="clearfix">
        <span>
          <i class="el-icon-position"></i>
            考勤信息
        </span>
          </div>

          <div class="el-flex el-box-column" style="margin: 5px;" >
          <el-table
            :data="list"
            border
            stripe
            highlight-current-row
            height="100%"
            style="width: 100%"
          >
            <el-table-column align="center" type="index" width="55" label="序号" />
            <!--所属组织-->
            <el-table-column width="250px" align="center" label="所属组织">
              <template slot-scope="scope">
                <span>{{ scope.row.orgName }}</span>
              </template>
            </el-table-column>

            <!--所属部门-->
            <el-table-column width="120px" align="center" label="所属部门">
              <template slot-scope="scope">
                <span>{{ scope.row.deptName }}</span>
              </template>
            </el-table-column>

            <!--员工编码-->
            <el-table-column width="80px"  align="center" label="员工编码">
              <template slot-scope="scope">
                <span>{{ scope.row.empCode }}</span>
              </template>
            </el-table-column>

            <!--员工姓名-->
            <el-table-column width="80px"  align="center" label="员工姓名">
              <template slot-scope="scope">
                <span>{{ scope.row.empName }}</span>
              </template>
            </el-table-column>

            <!--日期-->
            <el-table-column width="100px" align="center" label="日期">
              <template slot-scope="scope">
                <span>{{ scope.row.attendDate | parseTime('{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>

            <!--星期-->
            <el-table-column width="80px"  align="center" label="星期">
              <template slot-scope="scope">
                <span>{{ scope.row.weekDay }}</span>
              </template>
            </el-table-column>

            <!--考勤状态y正常，n异常-->
            <el-table-column
              prop="confirmStatus"
              :formatter="dictFormatter"
              label-class-name="confirmStatus"
              width="80px"
              label="考勤状态"
              align="center"
            >
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.confirmStatus }}</span>
              </template>-->
            </el-table-column>

            <!--事务类型-->
            <el-table-column width="80px" prop="holidayType" align="center" label="事务类型" :formatter="dictFormatter" label-class-name="paType">
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.holidayType }}</span>
              </template> -->
            </el-table-column>

            <!--时间段-->
            <el-table-column
              prop="timeType"
              :formatter="dictFormatter"
              label-class-name="timeType"
              width="100px"
              align="center"
              label="时间段"
            >
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.timeType }}</span>
              </template>-->
            </el-table-column>

            <!--应签到时间-->
            <el-table-column width="100px" align="center" label="应签到时间">
              <template slot-scope="scope">
                <span>{{ scope.row.startDate | parseTimeClock('{h}:{i}') }}</span>
              </template>
            </el-table-column>

            <!--应签退时间-->
            <el-table-column width="100px" align="center" label="应签退时间">
              <template slot-scope="scope">
                <span>{{ scope.row.endDate | parseTimeClock('{h}:{i}') }}</span>
              </template>
            </el-table-column>

            <!--签到时间-->
            <el-table-column width="80px" align="center" label="签到时间">
              <template slot-scope="scope">
                <!-- <span>{{ scope.row.signUp }}</span> -->
                <span>{{ scope.row.signUp | parseTimeClock('{h}:{i}') }}</span>
              </template>
            </el-table-column>

            <!--签退时间-->
            <el-table-column width="80px" align="center" label="签退时间">
              <template slot-scope="scope">
                <!-- <span>{{ scope.row.signOut }}</span> -->
                <span>{{ scope.row.signOut | parseTimeClock('{h}:{i}') }}</span>
              </template>
            </el-table-column>

            <!--签到状态-->
            <el-table-column
              prop="signUpStatus"
              :formatter="dictFormatter"
              label-class-name="signStatus"
              width="80px"
              label="签到状态"
              align="center"
            >
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.signUpStatus }}</span>
              </template>-->
            </el-table-column>

            <!--签退状态-->
            <el-table-column
              prop="signOutStatus"
              :formatter="dictFormatter"
              label-class-name="signStatus"
              width="80px"
              label="签退状态"
              align="center"
            >
              <!-- <template slot-scope="scope">
                <span>{{ scope.row.signOutStatus }}</span>
              </template>-->
            </el-table-column>

            <!--工作时长-->
            <el-table-column width="80px"  align="center" label="工作时长">
              <template slot-scope="scope">
                <span>{{ scope.row.workTime }}</span>
              </template>
            </el-table-column>

            <!--备注-->
            <el-table-column width="150px"  align="center" label="备注">
              <template slot-scope="scope">
                <span>{{ scope.row.kqMemo }}</span>
              </template>
            </el-table-column>

            <!--签到备注-->
            <el-table-column width="150px"  align="center" label="签到备注">
              <template slot-scope="scope">
                <span>{{ scope.row.signUpInfo }}</span>
              </template>
            </el-table-column>

            <!--签退备注-->
            <el-table-column width="150px"  align="center" label="签退备注">
              <template slot-scope="scope">
                <span>{{ scope.row.signOutInfo }}</span>
              </template>
            </el-table-column>

            <!--签到地点-->
            <el-table-column width="200px"  align="center" label="签到地点">
              <template slot-scope="scope">
                <span>{{ scope.row.signUpAddr }}</span>
              </template>
            </el-table-column>

            <!--签退地点-->
            <el-table-column width="200px"  align="center" label="签退地点">
              <template slot-scope="scope">
                <span>{{ scope.row.signOutAddr }}</span>
              </template>
            </el-table-column>
          </el-table>
          </div>
          <pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :page.sync="queryForm.page"
            :limit.sync="queryForm.limit"

            @pagination="getList"
          />
        </el-card>

      <!--进度条弹出begin-->
      <el-dialog
        :title="processObject.processTitle"
        :visible.sync="processObject.progressVisible"
        width="30%"
        top="30vh"
        append-to-body
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
      >
        <el-progress
          ref="progressRef"
          :text-inside="true"
          :stroke-width="18"
          :percentage="processObject.progressPercentage"
          :color="processObject.progressColors.percent100"
        />
      </el-dialog>
      <!--进度条弹出end-->
      <!-- table end -->
      </div>
  </el-card>
</template>

<script>
import {
  attendanceInfoList,
  rangeSyncAttendanceInfo
} from '@/api/attendance/info-service'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import dictCombo from './components/combo/dict-comb'
import OrgSelectTree from './components/combo/org-tree-select.vue'
import DeptSelectTree from './components/combo/dept-tree-select.vue'
import EmpCombobox from '@/views/attendance/components/combo/emp-comb'
import Vue from 'vue'

export default {
  components: { Pagination, dictCombo, OrgSelectTree, DeptSelectTree, EmpCombobox },
  data() {
    return {
      appPath:'',//这个是协同工作台获取到的参数默认值
      height: 'calc(100% - 16px)',
      msg: '-',
      text_form: '查询信息',
      text_grid: '考勤信息',
      list: [],
      listLoading: true,
      total: 0,
      empInputShow: true,
      processObject: {
        processTitle: '数据同步中，请耐心等待',
        progressPercentage: 0,
        progressVisible: false,
        progressColors: {
          percent20: '#f56c6c',
          percent50: '#6f7ad3',
          percent100: '#5cb87a'
        }
      },
      btn_sync_disabled: true,
      input_org_disabled: false,
      input_dept_disabled: false,
      input_emp_disabled: false,
      queryForm: {
        page: 1,
        limit: 20,
        ouId: '',
        orgCode: '',
        orgName: '',
        deptName: '',
        deptCode: '',
        empName: '',
        empCode: '',
        beginDate: null,
        endDate: null,
        confirmStatus: '',
        queryRange: ''
      },
      pickerOptionsStart: {
        disabledDate: (time) => {
          if (this.queryForm.endDate) {
            return time.getTime() > new Date(this.queryForm.endDate).getTime() - 1 * 24 * 60 * 60 * 1000
          }
          return false
        }
      },
      pickerOptionsEnd: {
        disabledDate: (time) => {
          return (time.getTime() < new Date(this.queryForm.beginDate).getTime() + 1 * 24 * 60 * 60 * 1000) || (time.getTime() > new Date(this.queryForm.beginDate).getTime() + 30 * 24 * 60 * 60 * 1000)
        }
      },
      queryFormRules: {
        beginDate: [{
          required: true,
          message: '开始日期不能为空',
          trigger: 'blur'
        }],
        endDate: [{
          required: true,
          message: '结束日期不能为空',
          trigger: 'blur'
        }]
      },
      syncQueryForm: {
        beginDate: new Date(),
        endDate: new Date()
      },
      inputRules: {}
    }
  },


  created() {

    this.appPath = this.$route.query.appPath; //  接收从协同工作台传递过来的参数

    // const queryRange = localStorage.getItem('queryRange')
    // this.queryForm.queryRange = queryRange
    // if (queryRange === 'all') {
    //   // donothing
    // } else if (queryRange === 'dept') {
    //   try {
    //     this.queryForm.deptName = localStorage.getItem('deptName')
    //     this.queryForm.deptCode = localStorage.getItem('deptCode')
    //   } catch (e) {
    //     console.log(e)
    //   }
    // } else if (queryRange === 'person') {
    //   try {
    //     this.queryForm.empCode = localStorage.getItem('empCode')
    //     this.queryForm.empName = localStorage.getItem('empName')
    //   } catch (e) {
    //     console.log(e)
    //   }
    // }
  },
  mounted() {
    const queryRange = localStorage.getItem('queryRange')
    this.queryForm.queryRange = queryRange
    //
    if (queryRange === 'all') {
      this.btn_sync_disabled = false
    } else if (queryRange === 'dept') {
      try {
        this.queryForm.orgCode = localStorage.getItem('orgCode')
        this.queryForm.orgName = localStorage.getItem('orgName')
        this.queryForm.deptName = localStorage.getItem('deptName')
        this.queryForm.deptCode = localStorage.getItem('deptCode')
      } catch (e) {
        console.log(e)
      }
      // donothing
    } else if (queryRange === 'person') {
      this.btn_sync_disabled = false
      try {
        this.queryForm.orgCode = localStorage.getItem('orgCode')
        this.queryForm.orgName = localStorage.getItem('orgName')
        this.queryForm.deptName = localStorage.getItem('deptName')
        this.queryForm.deptCode = localStorage.getItem('deptCode')
      } catch (e) {
        console.log(e)
      }

      // 如果是个人查询，屏蔽组织、部门、人员的查询条件
      this.input_org_disabled = true
      this.input_dept_disabled = true
      this.input_emp_disabled = true
      this.empInputShow = false
      try {
        const _this = this
        Vue.prototype.loginUser().then(function() {
          _this.queryForm.empCode = localStorage.getItem('empCode')
          _this.queryForm.empName = localStorage.getItem('empName')
          _this.getList()
        })
      } catch (e) {
        console.log(e)
      }
    }

    // const _this = this
    // Vue.prototype.loginUser().then(function() {
    //   _this.getList()
    // })
    this.getList()
  },
  methods: {

    // 判断是协同工作台过来的就会有返回按钮
    //  返回触发返回到上一页
    goBackWorkbench(){
      window.history.go(-1)
    },

    bindLabel(orgId, label) {
      if (orgId && orgId !== '') {
        this.queryForm.ouId = orgId
      }

      if (label && label !== '') {
        this.queryForm.orgName = label
      }
    },
    bindDeptLabel(orgId, label) {
      if (orgId && orgId !== '') {
        this.queryForm.deptId = orgId
      }

      if (label && label !== '') {
        this.queryForm.deptName = label
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getList() {
      this.listLoading = true
      attendanceInfoList(this.queryForm).then(response => {
        this.list = response.data.records
        this.total = response.data.total

        // 假loading 1秒后消失
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      })
    },
    onDataSync() { // 同步
      // this.$message({ type: 'success', message: '开始同步' })
      var _this = this

      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.processObject.progressVisible = true // 显示进度条

          // 定时增加进度条进度
          this.timer = setInterval(function() {
            _this.processObject.progressPercentage += 1 // 每次+1
            if (_this.processObject.progressPercentage > 98) {
              _this.processObject.progressPercentage = 98
              clearInterval(_this.timer) // 到98时停止增加
            }
          }, 500)

          rangeSyncAttendanceInfo(this.queryForm).then(response => {
            // 同步完成，指定进度条为100
            _this.processObject.progressPercentage = 100
            _this.processObject.processTitle = '同步完成'
            // 设定同步消失
            setTimeout(() => {
              _this.processObject.progressVisible = false
              _this.getList()
              _this.processObject = {
                processTitle: '数据同步中，请耐心等待',
                progressPercentage: 0,
                progressVisible: false,
                progressColors: {
                  percent20: '#f56c6c',
                  percent50: '#6f7ad3',
                  percent100: '#5cb87a'
                }
              }
            }, 1000)
          },
          error => {
            console.log('err' + error) // for debug
            // _this.$message.error(error.message)
            _this.processObject.progressVisible = false
            _this.getList()
            _this.processObject = {
              processTitle: '数据同步中，请耐心等待',
              progressPercentage: 0,
              progressVisible: false,
              progressColors: {
                percent20: '#f56c6c',
                percent50: '#6f7ad3',
                percent100: '#5cb87a'
              }
            }
          }
          )
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import "./index.scss";

/*.clearfix {*/
  /*height: 40px;*/
  /*line-height: 40px;*/
/*}*/

div.popContainer {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  color: white;
}
</style>

